﻿
using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using NPOI.HSSF.UserModel;
using OfficeOpenXml;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Threading.Tasks;

namespace GPS.Controllers.GPS_CapPhep
{
    public class GPS_GiayPhepDaoTaoGiaoVienController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public GPS.MODELS.ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();
            string userId = User.Identity.GetUserId();
            ApplicationUser _currentUser = dbUser.Users.Find(userId);
            return _currentUser;
        }


        #region Index
        public ActionResult Index(ViewModelDCPDVGTVT SearchModel, int? currentPage)
        {
            List<CrudModelDCPDVGTVT> model = new List<CrudModelDCPDVGTVT>();
            // check user
            string DVGTVT_MA = this.getCurrentUser().MA_TINHTP;
            ViewBag.checkuser = DVGTVT_MA;
            string getdvgtvt_Ma = GPS.DAL.Check_User.CheckUser(this.getCurrentUser().MA_TINHTP, SearchModel.pDVGTVT_MA);
            SearchModel.pDVGTVT_MA = getdvgtvt_Ma;

            //load 
            if ((!string.IsNullOrEmpty(SearchModel.pDVGTVT_MA) && SearchModel.Value_Search == "Search")
                || (DVGTVT_MA == "40" && SearchModel.Value_Search == "Search"))
            {
                model = Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_CAPPHEP_GETLIST_DCPGTVT(SearchModel.pDVGTVT_MA, "DT", SearchModel.pDVGTVT_ACTIVE);
                foreach (var currentEntity in model)
                {
                    currentEntity.DCP_DVGTVT_MA = currentEntity.DVGTVT_MA;
                    currentEntity.DCP_DVGTVT_MAQL = currentEntity.DVGTVT_MAQL;
                    currentEntity.DCP_DVGTVT_TEN = currentEntity.DVGTVT_TEN;
                    currentEntity.DCP_DVGTVT_TEL = currentEntity.DVGTVT_TEL;
                    currentEntity.DCP_DVGTVT_FAX = currentEntity.DVGTVT_FAX;
                    currentEntity.DCP_DVGTVT_ADD = currentEntity.DVGTVT_ADD;
                    currentEntity.DCP_DVGTVT_HANGGPLX = currentEntity.DVGTVT_HANGGPLX;
                    currentEntity.DCP_DVGTVT_LLDT_CU = currentEntity.DVGTVT_LLDT;
                    currentEntity.DCP_DVGTVT_LOAI = currentEntity.DVGTVT_LOAI;
                    currentEntity.DCP_SOGP_CU = currentEntity.DVGTVT_SOGP;
                    currentEntity.DCP_NGAYGP_CU = currentEntity.DVGTVT_NGAYGP;
                    currentEntity.DCP_NGAYHHGP_CU = currentEntity.DVGTVT_NGAYHHGP;
                }
            }
            ViewBag.TotalRow = model.Count();
            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            return View(SearchModel);
        }
        #endregion

        #region Detail
        public ActionResult _Detail(string id)
        {
            string _DVGTVT_MA = id;
            CrudModelDCPDVGTVT model = Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_CAPPHEP_GETITEM_DCPGTVT(_DVGTVT_MA, "DT", "");
            model.DCP_DVGTVT_MA = model.DVGTVT_MA;
            model.DCP_DVGTVT_MAQL = model.DVGTVT_MAQL;
            model.DCP_DVGTVT_TEN = model.DVGTVT_TEN;
            model.DCP_DVGTVT_TEL = model.DVGTVT_TEL;
            model.DCP_DVGTVT_FAX = model.DVGTVT_FAX;
            model.DCP_DVGTVT_ADD = model.DVGTVT_ADD;
            model.DCP_DVGTVT_HANGGPLX = model.DVGTVT_HANGGPLX;
            model.DCP_DVGTVT_LLDT_CU = model.DVGTVT_LLDT;
            model.DCP_DVGTVT_LOAI = model.DVGTVT_LOAI;
            model.DCP_SOGP_CU = model.DVGTVT_SOGP;
            model.DCP_NGAYGP_CU = model.DVGTVT_NGAYGP;
            model.DCP_NGAYHHGP_CU = model.DVGTVT_NGAYHHGP;
            model.DCP_STATUS = model.DVGTVT_ACTIVE;
            return PartialView("_Detail", model);
        }

        #endregion

        #region Chỉnh sửa cấp phép
        [HttpGet]
        public ActionResult _Edit(string id)
        {
            string _DVGTVT_MA = id;
            CrudModelDCPDVGTVT model = Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_CAPPHEP_GETEDIT_DCPGTVT(_DVGTVT_MA, "DT", "1");
            if (model!= null)
            {
                model.DCP_DVGTVT_MA = model.DVGTVT_MA;
                model.DCP_DVGTVT_MAQL = model.DVGTVT_MAQL;
                model.DCP_DVGTVT_TEN = model.DVGTVT_TEN;
                model.DCP_DVGTVT_TEL = model.DVGTVT_TEL;
                model.DCP_DVGTVT_FAX = model.DVGTVT_FAX;
                model.DCP_DVGTVT_ADD = model.DVGTVT_ADD;
                model.DCP_DVGTVT_HANGGPLX = model.DVGTVT_HANGGPLX;
                model.DCP_DVGTVT_LLDT_CU = model.DVGTVT_LLDT;
                model.DCP_DVGTVT_LOAI = model.DVGTVT_LOAI;
                model.DCP_SOGP_CU = model.DVGTVT_SOGP;
                model.DCP_NGAYGP_CU = model.DVGTVT_NGAYGP;
                model.DCP_NGAYHHGP_CU = model.DVGTVT_NGAYHHGP;
                model.DCP_STATUS = model.DVGTVT_ACTIVE;
                return PartialView("_Edit", model);
            }
            else
            {
                ViewBag.MessengerAler = "Hiện tại trung tâm sát hạch chưa có lịch sử cấp phép. Bạn cần phải thêm mới...!";
                return PartialView("_Alert", model);
            }
        }

        [HttpPost]
        public ActionResult _Edit(CrudModelDCPDVGTVT model, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                model.DCP_NGAYNHAP = DateTime.Now;
                model.DCP_NGUOINHAP = "admin";
                model.DCP_DVGTVT_LOAI = "DT";
                //CrudModelDCPDVGTVT model_ckh = Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_CAPPHEP_DCPDV_CHECKEXITS(model.DCP_DVGTVT_MA, "DT");
                //if (model_ckh == null)
                //{
                //    Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_DCPDVGTVT_INSERT(model);
                //}
                //else
                //{
                Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_DCPDVGTVT_UPDATE(model);
                //}
                string url = Url.Action("Index", "GPS_GiayPhepDaoTao");
                return Json(new { success = true, url = url });
            }
            else
            {
                return PartialView("_Edit", model);
            }
        }
        #endregion

        #region Thêm mới cấp phép
        [HttpGet]
        public ActionResult _Create(string id)
        {
            string _DVGTVT_MA = id;
            SDONVIGTVT _model = _db.Filter<SDONVIGTVT>(x => x.DVGTVT_MA == _DVGTVT_MA && x.DVGTVT_MAQL == "40").FirstOrDefault();

            //CrudModelDCPDVGTVT model = Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_CAPPHEP_GETITEM_DCPGTVT(_DVGTVT_MA, "DT", "");.
            CrudModelDCPDVGTVT model = new CrudModelDCPDVGTVT();
            model.DCP_DVGTVT_MA = _model.DVGTVT_MA;
            model.DCP_DVGTVT_MAQL = _model.DVGTVT_MAQL;
            model.DCP_DVGTVT_TEN = _model.DVGTVT_TEN;
            model.DCP_DVGTVT_TEL = _model.DVGTVT_TEL;
            model.DCP_DVGTVT_FAX = _model.DVGTVT_FAX;
            model.DCP_DVGTVT_ADD = _model.DVGTVT_ADD;
            model.DCP_DVGTVT_HANGGPLX = _model.DVGTVT_HANGGPLX;
            model.DCP_DVGTVT_LLDT_CU = _model.DVGTVT_LLDT;
            model.DCP_DVGTVT_LOAI = _model.DVGTVT_LOAI;
            model.DCP_SOGP_CU = _model.DVGTVT_SOGP;
            model.DCP_NGAYGP_CU = _model.DVGTVT_NGAYGP;
            model.DCP_NGAYHHGP_CU = _model.DVGTVT_NGAYHHGP;
            model.DCP_STATUS = _model.DVGTVT_ACTIVE;
            return PartialView("_Create", model);
        }

        [HttpPost]
        public ActionResult _Create(CrudModelDCPDVGTVT model, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                model.DCP_NGAYNHAP = DateTime.Now;
                model.DCP_NGUOINHAP = "admin";
                model.DCP_DVGTVT_LOAI = "DT";
                Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_DCPDVGTVT_INSERT(model);
                string url = Url.Action("Index", "GPS_GiayPhepDaoTao");
                return Json(new { success = true, url = url });
            }
            else
            {
                return PartialView("_Create", model);
            }
        }
        #endregion

        public ActionResult Export(string _DCP_DVGTVT_TEN, string _DCP_DVGTVT_MAQL, string _DCP_DVGTVT_MA, string _DCP_SOGP_MOI, string _DCP_NGAYGP_MOI, string _DCP_MOTA, string _DCP_SOVB_KT, string _DCP_NGAYVB_KT, string _DCP_SOVB_TT, string _DCP_NGAYVB_TT, string _DCP_DVGTVT_LLDT_MOI, string _DCP_STATUS)
        {
            try
            {
                CrudModelDCPDVGTVT _model = new CrudModelDCPDVGTVT();
                _model.DCP_DVGTVT_MA = _DCP_DVGTVT_MA;
                _model.DCP_SOGP_MOI = _DCP_SOGP_MOI;
                _model.DCP_NGAYGP_MOI = _DCP_NGAYGP_MOI == null ? (DateTime?)null : DateTime.Parse(_DCP_NGAYGP_MOI);
                _model.DCP_MOTA = _DCP_MOTA;
                _model.DCP_SOVB_KT = _DCP_SOVB_KT;
                _model.DCP_NGAYVB_KT = _DCP_NGAYVB_KT == null ? (DateTime?)null : DateTime.Parse(_DCP_NGAYVB_KT);
                _model.DCP_SOVB_TT = _DCP_SOVB_TT;
                _model.DCP_NGAYVB_TT = _DCP_NGAYVB_TT == null ? (DateTime?)null : DateTime.Parse(_DCP_NGAYVB_KT);
                _model.DCP_DVGTVT_LLDT_MOI = _DCP_DVGTVT_LLDT_MOI == null ? (decimal?)null : decimal.Parse(_DCP_DVGTVT_LLDT_MOI); ;
                _model.DCP_STATUS = _DCP_STATUS;
                _model.DCP_DVGTVT_TEN = _DCP_DVGTVT_TEN;
                _model.DCP_DVGTVT_MAQL = _DCP_DVGTVT_TEN;
                byte[] contentFile = null;
                contentFile = GPS.ENTITYS.EXPORTWORD_DCPDVGTVT.EXPORT_DCPDVGTVT(Server.MapPath("~/Templates/GPS_Word/GP_CoSoDaoTao.docx"), _model, "DocxGenericReport");
                string nameOutput = "Capphepdaotao_" + _model.DCP_DVGTVT_MA + "_" + DateTime.Now.ToShortDateString() + ".docx";
                return File(contentFile, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", nameOutput);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("ErrorMsg", ex.Message);
                return RedirectToAction("Index");
            }
        }
        public void initialCategorySearchActionss(ViewModelDCPGV viewModel)
        {
            ViewBag.GV_HANGDT_MAs = new SelectList(_db.Filter<SHANGDT>(x => x.HANGDT_RANK >= 5), "HANGDT_MA", "HANGDT_TEN");
            //string DVGTVT_MA = this.getCurrentUser().MA_TINHTP;
            //ViewBag.checkuser = DVGTVT_MA;
            //string getdvgtvt_Ma = GPS.DAL.Check_User.CheckUser(this.getCurrentUser().MA_TINHTP, viewModel.pDCP_GV_DVGTVT_MA);
            //viewModel.pDCP_GV_DVGTVT_MA = getdvgtvt_Ma;
        }
        public ActionResult Search(ViewModelDCPGV SearchModel, int? currentPage)
        {
            initialCategorySearchActionss(SearchModel);
            List<CrudModelDCPGV> model = new List<CrudModelDCPGV>();
            string DVGTVT_MA = this.getCurrentUser().MA_TINHTP;
            ViewBag.checkuser = DVGTVT_MA;

            string getdvgtvt_Ma = GPS.DAL.Check_User.CheckUser(this.getCurrentUser().MA_TINHTP, SearchModel.pDCP_GV_DVGTVT_MA);
            SearchModel.pDCP_GV_DVGTVT_MA = getdvgtvt_Ma;

            //if ((!string.IsNullOrEmpty(SearchModel.pDCP_GV_DVGTVT_MA) && SearchModel.ExportButton == "Search")
            //    || (DVGTVT_MA == "40" && SearchModel.ExportButton == "Search"))
            //{
            model = Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_CAPPHEP_DCPGV(SearchModel.pDCP_GV_DVGTVT_MA
                                                                                            , SearchModel.pDCP_GV_MA
                                                                                            , SearchModel.pDCP_GV_HODEM
                                                                                            , SearchModel.pDCP_GV_TEN
                                                                                            , SearchModel.pDCP_GV_HANGDT_MA
                                                                                            , SearchModel.pDCP_GV_SOCMT);
            Int16 icout = 1;
            foreach (var currentEntity in model)
            {
                currentEntity.STT = icout.ToString();
                icout++;
            }
            ViewBag.TotalRow = model.Count();
            //}

            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            return View("Search", SearchModel);
        }
    }
}